package com.lenovo.agricultureclient.ui.dialog;

import com.lenovo.agricultureclient.ClientApp;
import com.lenovo.agricultureclient.R;
import com.lenovo.agricultureclient.request.BaseRequest;
import com.lenovo.agricultureclient.request.RegisterRequest;
import com.lenovo.agricultureclient.request.BaseRequest.RequestResult;
import com.lenovo.agricultureclient.util.Util;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
 * 用户注册对话框
 * @author zongbingwu
 *
 */
public class UserRegisterDialog extends AppBaseDialog 
{
	//用户注册的请求对象
	private RegisterRequest mRegisterRequest;
    
    private EditText mUsernameET;
    private EditText mPasswordET;
    private EditText mEmailET;
    
    private Button mCloseBtn;
    private Button mPostBtn;
    
    public UserRegisterDialog(Activity act, ClientApp app)
    {
    	super(act,app);
    }
    
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        this.setContentView(R.layout.user_register_dialog);
        getWindow().setBackgroundDrawable(new ColorDrawable(0));
        initView();
        
        mRegisterRequest = new RegisterRequest();
        mRegisterRequest.setOnResponseEventListener(new BaseRequest.OnResponseEventListener() {
			@Override
			public void onResponse(BaseRequest request, RequestResult result) {
				//处理用户注册请求的结果
				dismissLoadDialog();
				if(mRegisterRequest.isSuccess()){
					//如果成功，则提示用户
					showAlertDialog(getString(R.string.prompt), getString(R.string.register_success));
					UserRegisterDialog.this.dismiss();
				} else {
					showAlertDialog(getString(R.string.prompt), getString(R.string.register_failed));
				}
			}
		});
    }  
  
    private void initView()
    {
    	mCloseBtn = (Button)findViewById(R.id.close);
    	mCloseBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				UserRegisterDialog.this.dismiss();
			}
		});
    	
    	mUsernameET = (EditText)findViewById(R.id.account_edit_text);
    	mPasswordET = (EditText)findViewById(R.id.password_edit_text);
    	mEmailET = (EditText)findViewById(R.id.email_edit_text);
    	
    	mPostBtn = (Button)findViewById(R.id.ok_button);
    	mPostBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				userRegisterEnter();
			}
		});
    }
    
    private void userRegisterEnter()
    {
    	String username = mUsernameET.getText().toString().trim();
    	String password = mPasswordET.getText().toString().trim();
    	String email = mEmailET.getText().toString().trim();
    	//检查用户名，密码和邮箱地址
    	if(username.equals("")){
    		showAlertDialog(getString(R.string.prompt), getString(R.string.username_not_empty));
    		return;
    	}
    	if(password.equals("")){
    		showAlertDialog(getString(R.string.prompt), getString(R.string.password_not_empty));
    		return;
    	}
    	if(email.equals("")){
    		showAlertDialog(getString(R.string.prompt), getString(R.string.email_not_empty));
    		return;
    	}
    	if(!Util.isEmail(email)){
    		showAlertDialog(getString(R.string.prompt), getString(R.string.invalid_email));
    		return;
    	}
    	
    	mRegisterRequest.setUsername(username);
    	mRegisterRequest.setPassword(password);
    	mRegisterRequest.setEmail(email);
    	//开启发起请求
    	startRequest(mRegisterRequest);
    	//显示等待提示框
    	showLoadDialog();
    }
}
